import request from '@/router/axios';
import Mock from 'mockjs';

let _list = [ 
    {
        brandStory: 'are you ok?',//品牌故事
        firstLetter: "M",//品牌首字母
        id: 6,           //id
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180518/5a912944N474afb7a.png",
        name: "小米",
        productCount: 260,  //数量
        showStatus: 1,     // 是否显示  0 不显示  1显示
    },
    {
        brandStory: 'BOOB的故事',
        firstLetter: "S",
        id: 49,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180518/1522738681.jpg",
        name: "七匹狼",
        productCount: 577,
        showStatus: 0,
    },
    {
        brandStory: '男人的衣柜',
        firstLetter: "H",
        id: 50,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/LOGO1024.png",
        name: "海澜之家",
        productCount: 566,
        showStatus: 1,
    },
    {
        brandStory: '苹果的故事，你就是下一个乔布斯',
        firstLetter: "A",
        id: 51,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg",
        name: "苹果",
        productCount: 755,
        showStatus: 1,
    },
    {
        brandStory: '我们的手机会爆炸哦',
        firstLetter: "S",
        id: 2,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (1).jpg",
        name: "三星",
        productCount: 900,
        showStatus: 1,
    },
    {
        brandStory: '中华有为',
        firstLetter: "H",
        id: 3,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (2).jpg",
        name: "华为",
        productCount: 508,
        showStatus: 0,
    },
    {
        brandStory: '世界第一',
        firstLetter: "G",
        id: 4,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (3).jpg",
        name: "格力",
        productCount: 800,
        showStatus: 0,
    },
    {
        brandStory: "智能橱柜",
        firstLetter: "F",
        id: 5,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (4).jpg",
        name: "方太",
        productCount: 590,
        showStatus: 0,
    },
    {
        brandStory: '万和的故事',
        firstLetter: "W",
        id: 1,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg",
        name: "万和",
        productCount: 180,
        showStatus: 1,
    },
    {
        brandStory: null,
        firstLetter: "O",
        id: 21,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(6).jpg",
        name: "OPPO",
        productCount: 888,
        showStatus: 1,
    },
    {
        brandStory: '运动时尚',
        firstLetter: "N",
        id: 58,
        logo: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/timg (51).jpg",
        name: "NIKE",
        productCount: 933,
        showStatus: 1,
    }
]

// brandStory,firstLetter,id,logo,name,productCount,showStatus

// 获取数据列表
export const brandList = (data) =>request({
    url:'/brandList',
    method:'get',
    data
})
Mock.mock(/\/brandList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

//添加品牌
export const addBrand = (data) =>request({
    url:'/addBrand',
    mothed:'post',
    data
});
Mock.mock(/\/addBrand/,config=>{
    var ids = Mock.mock({"id|1-50":1000});
    let datas = JSON.parse(config.body)
    let {name,productCount} = datas;
    if(name && productCount){
        _list.unshift({
            brandStory:datas.brandStory?datas.brandStory:'',
            firstLetter:datas.firstLetter?datas.firstLetter:'',
            id:ids.id,
            logo:datas.logo?datas.logo:'',
            name,
            productCount,
            showStatus:datas.showStatus?datas.showStatus:1
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除品牌
export const detBrand = (data) =>request({
    url:'/detBrand',
    mothed:'delete',
    data
})
Mock.mock(/\/detBrand/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        _list = _list.filter((item,index)=>item.id!=id)
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询品牌 根据名称
export const queryBrand = (data) =>request({
    url:'/queryBrand',
    mothed:'get',
    data
})
Mock.mock(/\/queryBrand/,config=>{
    let {name} = JSON.parse(config.body);
    let arr = [];
    if(name){
        arr = _list.filter((item,index)=>item.name.indexOf(name)>=0);
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// brandStory,firstLetter,id,logo,name,productCount,showStatus
// 修改品牌数据
export const editBrand = (data) =>request({
    url:'/editBrand',
    mothed:'put',
    data
})
Mock.mock(/\/editBrand/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.brandStory=datas.brandStory?datas.brandStory:item.brandStory
                item.firstLetter=datas.firstLetter?datas.firstLetter:item.firstLetter
                item.logo=datas.logo?datas.logo:item.logo
                item.name=datas.name?datas.name:item.name
                item.productCount=datas.productCount?datas.productCount:item.productCount
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//修改是否显示
export const editBrandShow = (data) =>request({
    url:'/editBrandShow',
    mothed:'put',
    data
})
Mock.mock(/\/editBrandShow/,config=>{
    let datas = JSON.parse(config.body);
    let {id,showStatus} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.showStatus=showStatus
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})
